package com.kyle.pagingdemo.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.kyle.pagingdemo.ArticleEntity
import com.kyle.pagingdemo.data.model.Page
import com.kyle.pagingdemo.data.model.Post

/**
 * author: liliang
 * date: 1/6/21 9:20 PM
 * desc:
 */
@Database(entities = [ArticleEntity::class, Page::class, Post::class], version = 1, exportSchema = false)
abstract class ArticleDatabase : RoomDatabase() {

    abstract fun articleDao(): ArticleDao

    companion object {
        @Volatile
        private var INSTANCE: ArticleDatabase? = null

        fun getDatabase(
            context: Context
        ): ArticleDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    ArticleDatabase::class.java,
                    "article_database"
                ).allowMainThreadQueries().build()
                INSTANCE = instance
                instance
            }
        }
    }

}